﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Silvermoon.Shapes
{
    /// <summary>
    /// Shape which contains several shapes which are ordered from left to right.
    /// </summary>
    public class StackShape : MultiShape
    {
        private int spacing = 10;

        public StackShape()
            : base()
        {
        }

        public StackShape(params Shape[] shapes)
            : base(shapes)
        {
            CalculateSize();
        }

        /// <summary>
        /// Gets the spacing in pixels between shapes. The default value is 10 pixel.
        /// </summary>
        public int Spacing
        {
            get { return spacing; }
            set
            {
                if (spacing != value)
                {
                    spacing = value;
                    Invalidate();
                }
            }
        }

        protected override void Layout()
        {
            base.Layout();
            CalculateSize();
        }

        private void CalculateSize()
        {
            int w = 0;
            int h = 0;
            foreach (Shape shape in Shapes)
            {
                int sw = shape.Width;
                if (sw > 0) sw += spacing;
                w += sw;
                h = Math.Max(shape.Height, h);
            }
            Size = new Size(w, h);
        }


        protected override void OnLayout()
        {
            int h = Size.Height;
            int x = 0;
            foreach (Shape shape in Shapes)
            {
                int y = (h - shape.Height) / 2;
                shape.Bounds = new Rectangle(x, y, shape.Width, Height);
                int w = shape.Width;
                if (w > 0) w += spacing;
                x += w;
            }
        }

        protected override Shape CreateClone()
        {
            return new StackShape();
        }

        protected override void Copy(Shape shape)
        {
            StackShape ss = shape as StackShape;
            ss.Spacing = spacing;
            base.Copy(shape);
        }

        protected override void OnShapesChanged()
        {
            CalculateSize();
            base.OnShapesChanged();
        }


    }
}
